package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleConfigMapper;
import com.heima.article.mapper.ArticleContentMapper;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleConfigMapper articleConfigMapper;
    @Autowired
    private ArticleContentMapper articleContentMapper;


    @Override
    public List<ApArticle> load(ArticleHomeDto dto, Short type) {
        //效验参数
        Integer size = dto.getSize();
        if (size == null || size <= 0 || size >= 1000){
            size = 10;
        }
        //效验频道 可能出现空 如果出现空默认加载首页 和前段约定_all_字符串
        String tag = dto.getTag();
        if (StringUtils.isBlank(tag)){
            tag = ArticleConstants.DEFAULT_TAG;
        }
                        //每页的大小 频道 最大时间 最小时间 类型
        Page<ApArticle> page = new Page<>(1, size);
        page.setSearchCount(false);
        return articleMapper.load(
                page,
                size,tag,dto.getMaxBehotTime(),dto.getMinBehotTime(),type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveArticle(ArticleDto dto) {
        if (dto.getId() == null){
            //新增
           /* ApArticle apArticle = new ApArticle();
            BeanUtils.copyProperties(dto,apArticle);*/
            save(dto);//主表数据
            //配置表
            ApArticleConfig apArticleConfig = new ApArticleConfig(dto.getId());
            articleConfigMapper.insert(apArticleConfig);
            //内容表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(dto.getId());
            articleContentMapper.insert(apArticleContent);

        }else {
            //修改
            updateById(dto);
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setContent(dto.getContent());
            apArticleContent.setArticleId(dto.getId());
            articleContentMapper.updateById(apArticleContent);

        }
        return dto.getId();
    }
}
